<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html class="x-admin-sm">

<head>
<meta charset="UTF-8">
<title>欢迎页面-X-admin2.2</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
<link rel="stylesheet" href="./css/font.css">
<link rel="stylesheet" href="./css/xadmin.css">
<script type="text/javascript" src="<%=basePath%>/lib/layui/layui.js"
	charset="utf-8"></script>
<script type="text/javascript" src="<%=basePath%>/js/xadmin.js"></script>
<!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
<!--[if lt IE 9]>
            <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
            <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
</head>

<body>
	<div class="layui-fluid">
		<div class="layui-row">
			<form class="layui-form">
				<div class="layui-form-item">
					<label for="username" class="layui-form-label"> <span
						class="x-red">*</span>姓名
					</label>
					<div class="layui-input-inline">
						<input type="text" id="stuName" name="stuName" required=""
							lay-verify="required" autocomplete="off" class="layui-input" value="${requestScope.stu.stuName }">
					</div>
				</div>
				<div class="layui-form-item">
                      <label class="layui-form-label"><span class="x-red">*</span>爱好</label>
                      <div class="layui-input-block" id="like">
                        <input type="checkbox" name="hobby" lay-skin="primary" title="篮球" value="篮球">
                        <input type="checkbox" name="hobby" lay-skin="primary" title="乒乓球" value="乒乓球">
                        <input type="checkbox" name="hobby" lay-skin="primary" title="足球" value="足球">
                      </div>
                  </div>
				<div class="layui-form-item">
					<label for="username" class="layui-form-label"> <span
						class="x-red">*</span>性别
					</label>
					<div class="layui-input-inline">
						<select id="gender" name="stuGender" class="valid">
							<c:choose>
								<c:when test="${stu.stuGender=='男' }">
									<option value="男" selected="selected">男</option>
									<option value="女">女</option>
								</c:when>
								<c:otherwise>
									<option value="男">男</option>
									<option value="女" selected="selected">女</option>
								</c:otherwise>
							</c:choose>
						</select>
					</div>
				</div>
				<div class="layui-form-item">
					<label for="phone" class="layui-form-label"> <span
						class="x-red">*</span>年龄
					</label>
					<div class="layui-input-inline">
						<input type="text" id="age" name="stuAge" lay-verify="required|Ndouble" 
							autocomplete="off" class="layui-input" value="${requestScope.stu.stuAge }">
					</div>
				</div>
				<div class="layui-form-item">
					<label for="username" class="layui-form-label"> <span
						class="x-red">*</span>出生日期
					</label>
					<div class="layui-input-inline">
						<input type="text" id="birthday" name="stuBirthDay"
							lay-verify="required" autocomplete="off" class="layui-input" 
							value="${requestScope.birthday }" readonly>
					</div>
				</div>
				<div class="layui-form-item">
					<label for="username" class="layui-form-label"> <span
						class="x-red">*</span>班级
					</label>
					<div class="layui-input-inline">
						<select name="classesId">
							<c:forEach items="${requestScope.cla_list }" var="cla">
								<c:choose>
		                            <c:when test="${stu.classesId==cla.id }">
		                            	<option value="${cla.id }" selected="selected">${cla.classesName }</option>
		                            </c:when>
		                            <c:otherwise>
		                            	<option value="${cla.id }">${cla.classesName }</option>
		                            </c:otherwise>
		                        </c:choose>
							</c:forEach>
						</select>
					</div>
				</div>
				<div class="layui-form-item">
					<label for="L_repass" class="layui-form-label"></label>
					<c:choose>
						<c:when test="${not empty stu.id }">
							<input type="hidden" name="id" value="${requestScope.stu.id }">
							<button class="layui-btn" lay-filter="add" lay-submit="">修改</button>
						</c:when>
						<c:otherwise>
							<button class="layui-btn" lay-filter="add" lay-submit="">增加</button>
						</c:otherwise>
					</c:choose>
				</div>
			</form>
		</div>
	</div>
	<script>layui.use(['form', 'layer', 'laydate'],
            function() {
                $ = layui.jquery;
                var form = layui.form,
                layer = layui.layer;
                var laydate = layui.laydate;
                //执行一个laydate实例
                laydate.render({
                    elem: '#birthday', //指定元素
                    type: 'datetime'
                });

                //自定义验证规则
                form.verify({
                    nikename: function(value) {
                        if (value.length < 5) {
                            return '昵称至少得5个字符啊';
                        }
                    },
                    pass: [/(.+){6,12}$/, '密码必须6到12位'],
                    repass: function(value) {
                        if ($('#L_pass').val() != $('#L_repass').val()) {
                            return '两次密码不一致';
                        }
                    },
                    Ndouble:[
                        /^[1-9]\d*$/
                        ,'只能输入整数哦'
                    ]
                });

                //监听提交
                form.on('submit(add)',
                function(data) {
                    let arr = [];
                    // 省略 input 标签的 name 的命名。
                    $('#like').find(':checkbox:checked').each((i, x) => {
                        arr.push($(x).val());
                    });
                    data.field['hobby'] = arr.toString();
                    console.log(data.filed);
                    //发异步，把数据提交给php
                    var url = 'doStudent?opr=saveOrUpdate';
					$.post(url, data.field, function(data) {
						if (data.code == 0) {
							layer.alert(data.msg, {
		                        icon: 6
		                    },
		                    function() {
		                        // 获得frame索引
		                        var index = parent.layer.getFrameIndex(window.name);
		                        //关闭当前frame
		                        parent.layer.close(index);
		                        //修改成功后刷新父界面
	                            window.parent.location.reload();
		                    });
						} else {
							layer.msg(data.msg);
						}
					}, 'json');
                    return false;
                });

            });</script>
</body>

</html>